package com.htfyun.fragment.App;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.htfyun.AppInfo;
import com.htfyun.fragment.base.BaseWithPageNumberAdapter;

import org.geometerplus.zlibrary.ui.android.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sst on 2017/11/28.
 */

public class AppAdapter extends BaseWithPageNumberAdapter<AppInfo, AppAdapter.MyViewHolder> {


    public AppAdapter(Context context) {
        super(context);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        List<AppInfo> items = getItems();

        if (items.size() <= position) {
            return;
        }

        AppInfo info = items.get(position);
        holder.imgIcon.setImageDrawable(info.getAppIcon());
        holder.txtAppName.setText(info.getAppLabel());
    }


    @Override
    public MyViewHolder getViewHolder(View view) {
        return new MyViewHolder(view);
    }

    @Override
    public int getLayoutId() {
        return R.layout.htfyun_app_adapter_item;
    }

    @Override
    public void onItemClick(int position, AppInfo appInfo) {

        srartApp(appInfo.getIntent());
    }


    public  void srartApp(Intent intent){
        if(intent!=null)
            //已安装包 直接启动
            getContext().startActivity(intent);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView txtAppName;
        ImageView imgIcon;

        public MyViewHolder(View itemView) {
            super(itemView);
            imgIcon = (ImageView) itemView.findViewById(R.id.imgIcon);
            txtAppName = (TextView) itemView.findViewById(R.id.txtAppName);
        }
    }


}
